import {
    getToken,
    setRefreshToken,
    setToken,
    setType,
} from '@/utils/auth'
import { appletLogin } from "@/api/index";
const user = {
    state: {
        token: getToken(),
        // baseurl: 'http://jgwh.natapp1.cc',//测试地址
        baseurl: 'https://billiard.leci.vip',
        paymentType:0,// 0先付费，1后付费
        storeId:'',
        role:''
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_PAYMENTTYPE: (state, paymentType) => {
            state.paymentType = paymentType
        },
        SET_STOREID: (state, storeId) => {
            state.storeId = storeId
        },
        SET_ROLE: (state, role) => {
            state.role = role
        },
    },

    actions: {
        // 用户名登录，获取token
        userLogin({ commit }, options) {
            return new Promise((resolve, reject) => {
                appletLogin(options).then((res) => {
                    if(res.statusCode == '00000'){
                        setToken(res.data.access_token)
                        commit('SET_TOKEN', res.data.access_token)
                        setType(res.data.paymentType)
                        commit('SET_PAYMENTTYPE', res.data.paymentType)
                        setRefreshToken(res.data.refresh_token)
                    }
                    resolve(res)
                }).catch((error) => {
                    reject(error)
                });
            })
        },

        //更新店铺id
        updateStoreId({commit},member){
            return new Promise((resolve, reject) => {
                commit('SET_STOREID', member)
                resolve(true)
            })
        },

        //更新权限
        updateRole({commit},member){
            return new Promise((resolve, reject) => {
                commit('SET_ROLE', member)
                resolve(true)
            })
        },
    }
}

export default user